// 负责创建、检索、销毁Session对象 同时处理会话存储
/*
使用唯一ID、SessionStorage
*/
#pragma once
#include "Session.hpp"
#include "SessionStorage.hpp"
#include "../http/HttpRequest.h"
#include "../http/HttpResponse.h"
#include <string>
#include <unordered_map>
#include <random>
namespace http
{
    namespace session
    {
        class SessionManager
        {
        private:
            std::string generateSessionId();//生成会话ID
            std::string getSessionIdfromCookie(const HttpRequest &request);//从Cookie中获取会话ID
            void setSessionIdtoCookie(HttpResponse &response, const std::string &session_id);//将会话ID设置到Cookie中
        public:
            //explicit 显式构造函数 禁止隐式转换
            explicit SessionManager(std::unique_ptr<SessionStorage> session_storage);
            std::shared_ptr<Session> getSession(const HttpRequest &request,HttpResponse &response);//获取会话
            void destroySession(const std::string &session_id);//销毁会话
            void updateSession(std::shared_ptr<Session> session);//更新会话
            void cleanExpiredSessions();//清理过期会话
        private:
            std::unique_ptr<SessionStorage> _session_storage;//保存会话的存储器
            std::mt19937 _randomIdGenerator;//随机数生成器
        };
    }
}